package com.hirayclay.incrementalprogressbar

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.View
import com.bumptech.glide.load.engine.DiskCacheStrategy
import kotlinx.android.synthetic.main.activity_main.*
import me.jessyan.progressmanager.ProgressListener
import me.jessyan.progressmanager.ProgressManager
import me.jessyan.progressmanager.body.ProgressInfo
import okhttp3.OkHttpClient
import java.lang.Thread.sleep

class MainActivity : AppCompatActivity() {
    private val TAG = "MainActivity"
    val imageUrl = "https://cdn.magdeleine.co/wp-content/uploads/2018/10/blooming-blossom-close-up-1260754-860x573.jpg"
    var p = 0f
    private lateinit var okClient: OkHttpClient

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        ProgressManager.getInstance().with(OkHttpClient.Builder()).build()
    }


    fun loadImageFromUrlString(v: View) {

        ProgressManager.getInstance().addResponseListener(imageUrl, object : ProgressListener {
            override fun onProgress(progressInfo: ProgressInfo) {
                val progress = progressInfo.currentbytes.toFloat() / progressInfo.contentLength
                progressbar.setProgress(progress)
                Log.i(TAG, "onProgress: ====== $progress  ${System.currentTimeMillis()}")
            }

            override fun onError(id: Long, e: Exception?) {
                Log.i(TAG, "onError: ")
            }

        })

        GlideApp.with(this)
                .load(imageUrl)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .into(image)


    }

    fun mock(v: View) {
        Thread({
            var p = 0f
            while (true) {
                p += 1.5f
                runOnUiThread({ progressbar.setProgress(p / 100) })
                sleep(1000)
            }
        }).start()
    }
}
